//
//  UIView+ACProperty.m
//  ACFramework
//
//  Created by caoyq on 2018/11/22.
//

#import "UIView+ACProperty.h"
#import <objc/runtime.h>

@implementation UIView (ACProperty)
static char ActionTag;

#pragma mark - Setter

- (void)setAc_x:(CGFloat)x
{
    CGRect frame = self.frame;
    frame.origin.x = x;
    self.frame = frame;
}

- (void)setAc_y:(CGFloat)y
{
    CGRect frame = self.frame;
    frame.origin.y = y;
    self.frame = frame;
}

- (void)setAc_bottom:(CGFloat)ac_bottom
{
    self.ac_y = ac_bottom - self.ac_height;
}

- (void)setAc_right:(CGFloat)ac_right {
    self.ac_x = ac_right - self.ac_width;
}

- (void)setAc_centerX:(CGFloat)ac_centerX {
    CGPoint center = self.center;
    center.x = ac_centerX;
    self.center = center;
}

- (void)setAc_centerY:(CGFloat)ac_centerY {
    CGPoint center = self.center;
    center.y = ac_centerY;
    self.center = center;
}

- (void)setAc_origin:(CGPoint)ac_origin {
    CGRect frame = self.frame;
    frame.origin = ac_origin;
    self.frame = frame;
}

- (void)setAc_width:(CGFloat)ac_width {
    CGRect frame = self.frame;
    frame.size.width = ac_width;
    self.frame = frame;
}

- (void)setAc_height:(CGFloat)ac_height {
    CGRect frame = self.frame;
    frame.size.height = ac_height;
    self.frame = frame;
}

- (void)setAc_size:(CGSize)ac_size {
    CGRect frame = self.frame;
    frame.size = ac_size;
    self.frame = frame;
}

#pragma mark - getter

- (CGFloat)ac_x {
    return self.frame.origin.x;
}

- (CGFloat)ac_y {
    return self.frame.origin.y;
}

- (CGFloat)ac_bottom {
    return self.frame.origin.y + self.frame.size.height;
}

- (CGFloat)ac_right {
    return self.frame.origin.x + self.frame.size.width;
}

- (CGFloat)ac_centerX {
    return self.center.x;
}

- (CGFloat)ac_centerY {
    return self.center.y;
}

- (CGPoint)ac_origin {
    return self.frame.origin;
}

- (CGFloat)ac_width {
    return self.frame.size.width;
}

- (CGFloat)ac_height {
    return self.frame.size.height;
}

- (CGSize)ac_size {
    return self.frame.size;
}

#pragma mark - 圆角属性

- (void)setAc_cornerRadius:(CGFloat)ac_cornerRadius
{
    objc_setAssociatedObject(self, @selector(ac_cornerRadius), @(ac_cornerRadius), OBJC_ASSOCIATION_ASSIGN);
    self.layer.cornerRadius = ac_cornerRadius;
    self.layer.masksToBounds = YES;
}

- (CGFloat)ac_cornerRadius
{
    return [objc_getAssociatedObject(self, _cmd) floatValue];
}

#pragma mark - 边框 + 颜色

- (void)setAc_borderWidth:(CGFloat)ac_borderWidth
{
    objc_setAssociatedObject(self, @selector(ac_borderWidth), @(ac_borderWidth), OBJC_ASSOCIATION_ASSIGN);
    self.layer.borderWidth = ac_borderWidth;
}

- (CGFloat)ac_borderWidth
{
    return [objc_getAssociatedObject(self, _cmd) floatValue];
}

- (void)setAc_borderColor:(UIColor *)ac_borderColor
{
    objc_setAssociatedObject(self, @selector(ac_borderColor), ac_borderColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    self.layer.borderColor = ac_borderColor.CGColor;
}

- (UIColor *)ac_borderColor
{
    return objc_getAssociatedObject(self, _cmd);
}

#pragma mark - actionBlock

- (void)ac_addAction:(ActionBlock)block
{
    objc_setAssociatedObject(self, &ActionTag, block, OBJC_ASSOCIATION_COPY_NONATOMIC);
    if ([self isKindOfClass:[UIButton class]]) {
        UIButton *btn = (UIButton *)self;
        [btn addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
    }
}

- (void)action:(id)sender
{
    ActionBlock blockAction = (ActionBlock)objc_getAssociatedObject(self, &ActionTag);
    if (blockAction)
    {
        blockAction((UIButton *)self);
    }
}

@end
